package com.example.yoho.mvp.view.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerShoesTrandComponent;
import com.example.yoho.di.moudles.ShoesTrandModules;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.ShoesTrandContract;
import com.example.yoho.mvp.model.entity.ShoesBean;
import com.example.yoho.mvp.presenter.ShoesTranPresenter;
import com.example.yoho.mvp.view.adapter.MyUfoAdapter;
import com.example.yoho.utils.MyGlide;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.view.CategoryHeadRefreshView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class UFOFragment extends BaseFragment<ShoesTranPresenter> implements ShoesTrandContract.View {


    private android.support.v7.widget.RecyclerView ufoRecyc;
    private List<ShoesBean.ValuesBean> list=new ArrayList<>();
    private MyUfoAdapter adapter;
    private ImageView imageView_as,imageView_bs,imageView_cs,imageView_ds,imageView_es,imageView_fs,imageView_gs;
    private Banner banner;
    private List<String> list_banner=new ArrayList<>();
    private String url="http://169.254.88.63/yoho/";
    private View view;
    private com.jwenfeng.library.pulltorefresh.PullToRefreshLayout ufoPull;

    @Override
    public int bondLayout() {
        return R.layout.fragment_ufo;
    }

    @Override
    public void Inject() {
        DaggerShoesTrandComponent.builder().shoesTrandModules(new ShoesTrandModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        ufoRecyc = (RecyclerView) findviewByIds(R.id.ufo_recyc);
         view = LayoutInflater.from(getContext()).inflate(R.layout.ufo_headview, null);
         imageView_as=view.findViewById(R.id.ufo_imageas);
         imageView_bs=view.findViewById(R.id.ufo_imagebs);
         imageView_cs=view.findViewById(R.id.ufo_imagecs);
         imageView_ds=view.findViewById(R.id.ufo_imageds);
         imageView_es=view.findViewById(R.id.ufo_imagees);
         imageView_fs=view.findViewById(R.id.ufo_imagefs);
         imageView_gs=view.findViewById(R.id.ufo_imagegs);
         banner=view.findViewById(R.id.ufo_banners);
         ufoPull = (PullToRefreshLayout) findviewByIds(R.id.ufo_pull);


    }

    @Override
    public void initData() {
        mPresenter.requestShoesAll();
        adapter=new MyUfoAdapter(R.layout.ufo_item,list);
        ufoRecyc.setAdapter(adapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        gridLayoutManager.setSpanSizeLookup(new SpanSizeLooksup());
        ufoRecyc.setLayoutManager(gridLayoutManager);
        adapter.addHeaderView(view);
        myufopull();
    }

    private void myufopull() {
        ufoPull.setHeaderView(new CategoryHeadRefreshView(getContext()));
        ufoPull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                list.clear();
                list_banner.clear();
                adapter.notifyDataSetChanged();
                mPresenter.requestShoesAll();
            }

            @Override
            public void loadMore() {
                mPresenter.requestShoesAll();
                ufoPull.finishLoadMore();
            }
        });
    }

    @Override
    public void showShoesData(ShoesBean shoesBean) {
        List<ShoesBean.ValuesBean> values = shoesBean.getValues();
        list.addAll(values);
        adapter.notifyDataSetChanged();
        for (int i = 0; i < shoesBean.getBanner().size(); i++) {
            list_banner.add(url+shoesBean.getBanner().get(i).getRecommend_url());
        }
        banner.setImages(list_banner);
        ufoimageloader();
        banner.start();
        ufoPull.finishLoadMore();
        ufoPull.finishRefresh();
        List<ShoesBean.BrandBean> brand = shoesBean.getBrand();
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(0).getBrand_icon(),imageView_as);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(1).getBrand_icon(),imageView_bs);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(2).getBrand_icon(),imageView_cs);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(3).getBrand_icon(),imageView_ds);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(4).getBrand_icon(),imageView_es);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(5).getBrand_icon(),imageView_fs);
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+brand.get(6).getBrand_icon(),imageView_gs);

    }

    private void ufoimageloader() {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
               MyGlide.GlideAS(context,path,imageView);
            }
        });
    }
    class SpanSizeLooksup extends GridLayoutManager.SpanSizeLookup {

        @Override
        public int getSpanSize(int i) {
            if (i==0){
                return 2;
            }else {
                return 1;
            }
        }
    }
}
